<?php
	//TBC: ham tao combo - ten bang, ten truong la gia tri, ten truong hien thi
	//dieu kien, ten cua combo trong form, index la gia tri ban dau duoc hien thi tren combo
function createCombo($table, $valueField, $textField, $where="", $name, $index){

	global $conn;

	$ret = array();

	//$hide = "status=0";

	$where = $where!="" ? $where : "1=1";

	$result = @mysql_query("select $valueField,$textField from $table where $where",$conn);
	
	echo '<select size="1" name="'.$name.'">';
	
	while($row=@mysql_fetch_assoc($result)){

		//$ret[] = array($row[$valueField],$row[$textField]);
		
		$selected = $row[$valueField] == $index ? 'selected' : '';
		
		echo "<option value = '".$row[$valueField]."' ".$selected.">".$row[$textField]."</option>";
	}

	echo '</select>';
 }
?>
<script type="text/javascript" src="../Scripts/calendar.js"></script>
<script type="text/javascript" src="../Scripts/calendar-setup.js"></script>
<script type="text/javascript" src="../Scripts/calendar-en.js"></script>
<link rel="stylesheet" href="../Styles/calendar-win2k-2.css" />
<script type="text/javascript" src="../lib/fckeditor2/fckeditor.js"></script>
    <script type="text/javascript">
      window.onload = function()
      {
        var oFCKeditor = new FCKeditor('txtVenue') ;
        oFCKeditor.BasePath = "../lib/fckeditor2/" ;
		oFCKeditor.Width = "720" ; 
		oFCKeditor.Height = "150" ; 
        oFCKeditor.ReplaceTextarea() ;
		
        var oFCKeditor1 = new FCKeditor('txtDescription') ;
        oFCKeditor1.BasePath = "../lib/fckeditor2/" ;
		oFCKeditor1.Width = "720" ; 
		oFCKeditor1.Height = "200" ; 
        oFCKeditor1.ReplaceTextarea() ;
      }
</script>
<? // Config
$request = substr($_REQUEST['act'],0,-2);
$codeSelect = $multiLanguage == 0 ? "code='".$_lang."_".$request."'" : "code='vn_".$request."' or code='en_".$request."'";
$tableCategoryConfig = 'tbl_content_category';
$tableConfig         = 'tbl_khaigiang';
$actConfig           = $request;
$firstWhere          = "parent in (select id from $tableCategoryConfig where $codeSelect)";
$arraySourceCombo    = getArrayCombo($tableCategoryConfig,'id','name',$codeSelect);
?>

<?
if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE'))
	echo '<script language="javascript" src="../lib/scripts/editor.js"></script>';
else
	echo '<script language="javascript" src="../lib/scripts/moz/editor.js"></script>'?>

<script language="javascript">
function btnSave_onclick(){
	if(test_empty(document.frmForm.txtName.value)){
		alert('Hãy nhập "tên" !');
		document.frmForm.txtName.focus();
		return false;
	}
	if(test_integer(document.frmForm.txtSort.value)){
		alert('"Thứ tự sắp xếp" phải là số !');
		document.frmForm.txtSort.focus();
		return false;
	}
	
	return true;
}
</script>

<? $errMsg =''?>
<?
$path = "../images/content";
$pathdb = "images/content";
if (isset($_POST['btnSave'])){
	//$code          = isset($_POST['txtCode']) ? trim($_POST['txtCode']) : '';
	$name          = isset($_POST['txtName']) ? trim($_POST['txtName']) : '';
	$parent        = $_POST['ddCat'];
	$thoigian 	   = isset($_POST['txtTime']) ? trim($_POST['txtTime']) : '';
	$chinhanh        = $_POST['ddChiNhanh'];
	$diadiem 	   = isset($_POST['txtVenue']) ? trim($_POST['txtVenue']) : '';
	$detail        = isset($_POST['txtDescription']) ? trim($_POST['txtDescription']) : '';
	$sort          = isset($_POST['txtSort']) ? trim($_POST['txtSort']) : 0;
	$status        = $_POST['chkStatus']!='' ? 1 : 0;
	
	$catInfo       = getRecord($tableCategoryConfig, 'id='.$parent);
	//$lang          = $catInfo['lang'];
	
	if ($name=="") $errMsg .= "Hãy nhập tên danh mục !<br>";
	
	if ($errMsg==''){
		if (!empty($_POST['id'])){
			$oldid = $_POST['id'];
			$sql = "update ".$tableConfig." set title='".$name."', parent='".$parent."',ngaykg='".$thoigian."',description='".$detail."', branch=".$chinhanh.", venue='".$diadiem."', sort='".$sort."', status='".$status."' where id='".$oldid."'";
		}else{
			$sql = "insert into ".$tableConfig." (title, ngaykg, branch, venue, parent, description, status) values ('".$name."','".$thoigian."',".$chinhanh.",'".$diadiem."',".$parent.",'".$detail."', ".$status.")";
		}
		//echo $sql;
		if (mysql_query($sql,$conn)){
			if(empty($_POST['id'])) $oldid = mysql_insert_id();
			$r = getRecord($tableConfig,"id=".$oldid);
			
			$sqlUpdateField = "";
			
			if ($_POST['chkClearImg']==''){
				$extsmall=getFileExtention($_FILES['txtImage']['name']);
				if (makeUpload($_FILES['txtImage'],"$path/".$actConfig."_s".$oldid.$extsmall)){
					@chmod("$path/".$actConfig."_s".$oldid.$extsmall, 0777);
					$sqlUpdateField = " image='$pathdb/".$actConfig."_s".$oldid.$extsmall."' ";
				}
			}else{
				if(file_exists('../'.$r['image'])) @unlink('../'.$r['image']);
				$sqlUpdateField = " image='' ";
			}
			
			if ($_POST['chkClearImgLarge']==''){
				$extlarge=getFileExtention($_FILES['txtImageLarge']['name']);
				if (makeUpload($_FILES['txtImageLarge'],"$path/".$actConfig."_l".$oldid.$extlarge)){
					@chmod("$path/".$actConfig."_l".$oldid.$extlarge, 0777);
					if($sqlUpdateField != "") $sqlUpdateField .= ",";
					$sqlUpdateField .= " image_large='$pathdb/".$actConfig."_l".$oldid.$extlarge."' ";
				}
			}else{
				if(file_exists('../'.$r['image_large'])) @unlink('../'.$r['image_large']);
				if($sqlUpdateField != "") $sqlUpdateField .= ",";
				$sqlUpdateField .= " image_large='' ";
			}
			
			if($sqlUpdateField!='')	{
				$sqlUpdate = "update ".$tableConfig." set $sqlUpdateField where id='".$oldid."'";
				mysql_query($sqlUpdate,$conn);
			}
		}else{
			$errMsg = "Không thể cập nhật !";
		}
	}

	if ($errMsg == '')
		echo '<script>window.location="./?act='.$actConfig.'&cat='.$_REQUEST['cat'].'&page='.$_REQUEST['page'].'&code=1"</script>';
}else{
	if (isset($_GET['id'])){
		$oldid=$_GET['id'];
		$page = $_GET['page'];
		$sql = "select * from ".$tableConfig." where id='".$oldid."'";
		if ($result = mysql_query($sql,$conn)) {
			$row=mysql_fetch_array($result);
			$name          = $row['title'];
			$parent        = $row['parent'];
			$thoigian  = $row['ngaykg'];
			$chinhanh        = $row['branch'];
			$sort          = $row['sort'];
			$status        = $row['status'];
			$diadiem    = $row['venue'];
			$detail = $row['description'];
		}
	}
	else	//ThanhBC: bo sung load ban dau cho txtTime lay thoi gian hien tai
	{
		$thoigian	=	date('Y-m-d');
	}
}

?>
<pre id="idTemporary0" name="idTemporary0" style="display:none">
<? if(isset($subject)){echo $detail_short;}?>
</pre>

<pre id="idTemporary1" name="idTemporary1" style="display:none">
<? if(isset($detail_short)){echo $detail;}?>
</pre>
<form method="post" name="frmForm" enctype="multipart/form-data" action="./">
<input type="hidden" name="act" value="<?=$actConfig?>_m">
<input type="hidden" name="id" value="<?=$_REQUEST['id']?>">
<input type="hidden" name="page" value="<?=$_REQUEST['page']?>">
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#0069A8" width="100%">
	
        	
				<tr>
					<td width="15%" class="smallfont" align="right">Tiêu đề</td>
					<td width="1%" class="smallfont" align="center"><font color="#FF0000">*</font></td>
					<td width="83%" class="smallfont">
						<input value="<?=$name?>" type="text" name="txtName" class="textbox" size="34">
					</td>
				</tr>
				
				
				<tr>
					<td width="15%" class="smallfont" align="right">Thời gian</td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
						<input type="text" name="txtTime" id="txtTime" value="<? echo date('Y-m-d',strtotime($thoigian));//date('Y-m-d',$thoigian)?>" />
						&nbsp;
						<img src="../Images/calendar.gif" border="0" id="date_picker_from" title="Date selector" style="cursor:pointer;" >
						<script type="text/javascript">
							Calendar.setup({
								inputField : "txtTime", // id of the input field
								ifFormat : "%Y-%m-%d", // format of the input field
								button : "date_picker_from", // trigger for the calendar (button ID)
								align : "Tl", // alignment (defaults to "Bl")
								singleClick : true
							});
						</script>
					</td>
				</tr>
				<tr>
					<td width="15%" class="smallfont" align="right">Chi nhánh</td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
						<?createCombo('tbl_branch', 'id', 'name', '', 'ddChiNhanh', $chinhanh)?>
					</td>
				</tr>
				
				<tr>
					<td width="15%" class="smallfont" align="right">Địa điểm</td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
						<textarea name="txtVenue" cols="80" rows="10" id="txtVenue"><?=$diadiem?></textarea>
					</td>				
				</tr>
				<tr>
					<td width="15%" class="smallfont" align="right">Thông tin chi tiết</td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
						<textarea name="txtDescription" cols="80" rows="10" id="txtDescription"><?=$detail?></textarea>
					</td>
				</tr>
				
				<tr>
					<td width="15%" class="smallfont" align="right">Thuộc danh mục</td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
						<?=comboCategory('ddCat',$arraySourceCombo,'smallfont',$parent,0)?>
					</td>
				</tr>
				
				<tr>
					<td width="15%" class="smallfont" align="right">Thứ tự sắp xếp</td>
					<td width="1%" class="smallfont" align="right"></td>
					<td width="83%" class="smallfont">
						<input value="<?=$sort?>" type="text" name="txtSort" class="textbox" size="34">
					</td>
				</tr>
				
				<tr>
					<td width="15%" class="smallfont" align="right">Không hiển thị</td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
						<input type="checkbox" name="chkStatus" value="on" <? if ($status>0) echo 'checked' ?>>
					</td>
				</tr>
				
				<tr>
					<td width="15%" class="smallfont"></td>
					<td width="1%" class="smallfont" align="center"></td>
					<td width="83%" class="smallfont">
						<input type="submit" name="btnSave" VALUE="Cập nhật" class=button onclick="return btnSave_onclick()">
						<input type="reset" class=button value="Nhập lại">
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
</form>
<? if($errMsg!=''){echo '<p align=center class="err">'.$errMsg.'<br></p>';}?>